package com.RMP
{
    import flash.display.*;
    import flash.events.*;
    import com.greensock.*;

    public class NavMenu extends Sprite
    {
        var myXMLList:XMLList;
        var btnsArray:Array;
        var navMenu:NavMenu;
        var fadeSpeed:Number;
        public var menuType:String;
        var navMenuArray:Array;
        var menuList:XMLList;
        public var isNavMenu:Boolean;
        public var currNavId:int;
        var myRoot:MovieClip;
        var labelList:XMLList;
        var XMLLength:int;
        var myTier:String;
        var newsMenu:NewsMenu;
        var navItem:NavItem;
		
        var bookmarksMenu:BookmarksMenu;
        var alphabetMenu:AlphabetMenu;
        var datePaginatedMenu:DatePaginatedMenu;
        var paginatedMenu:PaginatedMenu;
        var triPaginatedMenu:TriPaginatedMenu;

        public function NavMenu(param1:MovieClip, param2:Number, param3:String, param4:String, param5:XMLList) : void
        {
            menuType = "NavMenu";
            visible = false;
            alpha = 0;
            myRoot = param1;
            fadeSpeed = param2;
            isNavMenu = true;
            myXMLList = param5;
            XMLLength = myXMLList.length();
            labelList = new XMLList(myXMLList.@label);
            menuList = new XMLList(myXMLList.@menu);
            myTier = param3;
            this.name = param4;
            navMenuArray = new Array();
            identifyCol();
            myRoot.stage.addEventListener(Event.RESIZE, onResizer, false, 0, true);
            return;
        }

        function autoClick() : void
        {
            if (btnsArray == null)
            {
            }
            switchCols(currNavId);
            return;
        }

        public function fadeOutShell() : void
        {
            TweenMax.to(this, fadeSpeed, {alpha:0, overwrite:true, onComplete:function ()
            {
                visible = false;
                return;
            }
            });
            return;
        }

        function doResize() : void
        {
            var _loc_1:int = 0;
            var _loc_2:int = 0;
            if (navMenuArray.length > 0)
            {
                _loc_1 = 0;
                while (_loc_1 < navMenuArray.length)
                {
                    
                    if (navMenuArray[_loc_1] != undefined)
                    {
                        if (name == "countryMenu" && navMenuArray[_loc_1].menuType == "PaginatedMenu")
                        {
                            navMenuArray[_loc_1].x = Math.round(2 * (myRoot.unitW + myRoot.gutterW));
                        }
                        else
                        {
                            navMenuArray[_loc_1].x = Math.round(myRoot.unitW + myRoot.gutterW);
                        }
                    }
                    _loc_1++;
                }
            }
            if (btnsArray != null)
            {
                _loc_2 = 0;
                while (_loc_2 < btnsArray.length)
                {
                    
                    if (btnsArray[_loc_2].field != undefined)
                    {
                        var _loc_3:* = myRoot.unitW / 75;
                        btnsArray[_loc_2].field.scaleY = myRoot.unitW / 75;
                        btnsArray[_loc_2].field.scaleX = _loc_3;
                    }
                    btnsArray[_loc_2].bg.width = myRoot.unitW;
                    btnsArray[_loc_2].bg.height = btnsArray[_loc_2].field.height;
                    btnsArray[_loc_2].x = 0;
                    btnsArray[_loc_2].y = Math.round(14 * _loc_2 * myRoot.unitW / 75);
                    _loc_2++;
                }
            }
            return;
        }

        function buildSubCol(param1:int) : void
        {
            switch(myTier)
            {
                case "tier0":
                {
                    if (myXMLList[param1].@menu == "paginatedMenu")
                    {
                        paginatedMenu = new PaginatedMenu(myRoot, fadeSpeed, myXMLList[param1].@label, myXMLList[param1].item);
                        navMenuArray[param1] = paginatedMenu;
                        addChild(paginatedMenu);
                    }
                    else if (myXMLList[param1].@menu == "triPaginatedMenu")
                    {
                        triPaginatedMenu = new TriPaginatedMenu(myRoot, fadeSpeed, myXMLList[param1].@label, myXMLList[param1].tier1);
                        navMenuArray[param1] = triPaginatedMenu;
                        addChild(triPaginatedMenu);
                    }
                    else if (myXMLList[param1].@menu == "pressMenu")
                    {
                        navMenu = new NavMenu(myRoot, fadeSpeed, "tier1", myXMLList[param1].@menu, myXMLList[param1].tier1);
                        navMenuArray[param1] = navMenu;
                        navMenu.visible = false;
                        navMenu.alpha = 0;
                        addChild(navMenu);
                    }
                    else if (myXMLList[param1].@menu == "generalInfoMenu")
                    {
                        paginatedMenu = new PaginatedMenu(myRoot, fadeSpeed, myXMLList[param1].@label, myXMLList[param1].item);
                        navMenuArray[param1] = paginatedMenu;
                        addChild(paginatedMenu);
                    }
                    else if (myXMLList[param1].@menu == "employMenu")
                    {
                        navMenu = new NavMenu(myRoot, fadeSpeed, "tier1", myXMLList[param1].@menu, myXMLList[param1].tier1);
                        navMenuArray[param1] = navMenu;
                        navMenu.visible = false;
                        navMenu.alpha = 0;
                        addChild(navMenu);
                    }
                    else if (myXMLList[param1].@menu == "publicationsMenu")
                    {
                        navMenu = new NavMenu(myRoot, fadeSpeed, "tier1", myXMLList[param1].@menu, myXMLList[param1].tier1);
                        navMenuArray[param1] = navMenu;
                        navMenu.visible = false;
                        navMenu.alpha = 0;
                        addChild(navMenu);
                    }
                    else if (myXMLList[param1].@menu == "bookmarksMenu")
                    {
                        bookmarksMenu = new BookmarksMenu(myRoot, fadeSpeed, myXMLList[param1].@menu, myXMLList[param1].@label);
                        navMenuArray[param1] = bookmarksMenu;
                        bookmarksMenu.visible = false;
                        bookmarksMenu.alpha = 0;
                        addChild(bookmarksMenu);
                    }
                    else
                    {
                        navMenu = new NavMenu(myRoot, fadeSpeed, "tier1", myXMLList[param1].@menu, myXMLList[param1].tier1);
                        navMenuArray[param1] = navMenu;
                        navMenu.visible = false;
                        navMenu.alpha = 0;
                        addChild(navMenu);
                    }
                    break;
                }
                case "tier1":
                {
                    if (name == "publicationsMenu")
                    {
                        if (myXMLList[param1].@menu == "recentMenu")
                        {
                            paginatedMenu = new PaginatedMenu(myRoot, fadeSpeed, myXMLList[param1].@label, myXMLList[param1].item);
                            navMenuArray.push(paginatedMenu);
                            addChild(paginatedMenu);
                        }
                        else if (myXMLList[param1].@menu == "triPaginatedMenu")
                        {
                            triPaginatedMenu = new TriPaginatedMenu(myRoot, fadeSpeed, myXMLList[param1].@label, myXMLList[param1].tier3);
                            navMenuArray.push(triPaginatedMenu);
                            addChild(triPaginatedMenu);
                        }
                    }
                    else if (myXMLList[param1].@menu == "alphabetMenu")
                    {
                        alphabetMenu = new AlphabetMenu(myRoot, fadeSpeed, myXMLList[param1].@menu, myXMLList[param1].tier2);
                        navMenuArray[param1] = alphabetMenu;
                        addChild(alphabetMenu);
                    }
                    else if (myXMLList[param1].@menu == "paginatedMenu")
                    {
                        paginatedMenu = new PaginatedMenu(myRoot, fadeSpeed, myXMLList[param1].@label, myXMLList[param1].item);
                        navMenuArray[param1] = paginatedMenu;
                        addChild(paginatedMenu);
                    }
                    else if (myXMLList[param1].@menu == "datePaginatedMenu")
                    {
                        datePaginatedMenu = new DatePaginatedMenu(myRoot, fadeSpeed, myXMLList[param1].@label, myXMLList[param1].tier2);
                        navMenuArray[param1] = datePaginatedMenu;
                        addChild(datePaginatedMenu);
                    }
                    else if (myXMLList[param1].@menu == "publicationsMenu")
                    {
                        navMenu = new NavMenu(myRoot, fadeSpeed, "tier2", myXMLList[param1].@menu, myXMLList[param1].tier2);
                        navMenuArray[param1] = navMenu;
                        navMenu.visible = false;
                        navMenu.alpha = 0;
                        addChild(navMenu);
                    }
                    else
                    {
                        navMenu = new NavMenu(myRoot, fadeSpeed, "tier2", myXMLList[param1].@menu, myXMLList[param1].tier2);
                        navMenuArray[param1] = navMenu;
                        navMenu.visible = false;
                        navMenu.alpha = 0;
                        addChild(navMenu);
                    }
                    break;
                }
                case "tier2":
                {
                    switch(name)
                    {
                        case "typeMenu":
                        {
                            paginatedMenu = new PaginatedMenu(myRoot, fadeSpeed, myXMLList[param1].@label, myXMLList[param1].item);
                            navMenuArray.push(paginatedMenu);
                            addChild(paginatedMenu);
                            break;
                        }
                        case "dateMenu":
                        {
                            if (myXMLList[param1].@menu == "datePaginatedMenu")
                            {
                                datePaginatedMenu = new DatePaginatedMenu(myRoot, fadeSpeed, myXMLList[param1].@label, myXMLList[param1].tier3);
                                navMenuArray.push(datePaginatedMenu);
                                addChild(datePaginatedMenu);
                            }
                            else if (myXMLList[param1].@menu == "paginatedMenu")
                            {
                                paginatedMenu = new PaginatedMenu(myRoot, fadeSpeed, myXMLList[param1].@label, myXMLList[param1].item);
                                navMenuArray.push(paginatedMenu);
                                addChild(paginatedMenu);
                            }
                            break;
                        }
                        case "contactMenu":
                        {
                            break;
                        }
                        case "continentMenu":
                        {
                            navMenu = new NavMenu(myRoot, fadeSpeed, "tier3", myXMLList[param1].@menu, myXMLList[param1].tier3);
                            navMenuArray.push(navMenu);
                            navMenu.visible = false;
                            navMenu.alpha = 0;
                            addChild(navMenu);
                            break;
                        }
                        case "paginatedMenu":
                        {
                            break;
                        }
                        case "publicationsMenu":
                        {
                            if (myXMLList[param1].@menu == "recentMenu")
                            {
                                paginatedMenu = new PaginatedMenu(myRoot, fadeSpeed, myXMLList[param1].@label, myXMLList[param1].item);
                                navMenuArray.push(paginatedMenu);
                                addChild(paginatedMenu);
                            }
                            else if (myXMLList[param1].@menu == "triPaginatedMenu")
                            {
                                triPaginatedMenu = new TriPaginatedMenu(myRoot, fadeSpeed, myXMLList[param1].@label, myXMLList[param1].tier3);
                                navMenuArray.push(triPaginatedMenu);
                                addChild(triPaginatedMenu);
                            }
                            break;
                        }
                        case "triPaginatedMenu":
                        {
                            break;
                        }
                        case "staffSubMenu":
                        {
                            paginatedMenu = new PaginatedMenu(myRoot, fadeSpeed, myXMLList[param1].@label, myXMLList[param1].item);
                            navMenuArray.push(paginatedMenu);
                            addChild(paginatedMenu);
                            break;
                        }
                        default:
                        {
                            break;
                        }
                    }
                    break;
                }
                case "tier3":
                {
                    switch(name)
                    {
                        case "countryMenu":
                        case "paginatedMenu":
                        {
                            paginatedMenu = new PaginatedMenu(myRoot, fadeSpeed, myXMLList[param1].@label, myXMLList[param1].tier4);
                            navMenuArray[param1] = paginatedMenu;
                            addChild(paginatedMenu);
                            break;
                        }
                        default:
                        {
                            break;
                        }
                    }
                    break;
                }
                case "tier4":
                {
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }

        function buildCol() : void
        {
            var _loc_1:int = 0;
            btnsArray = new Array();
            _loc_1 = 0;
            while (_loc_1 < XMLLength)
            {
                
                btnsArray[_loc_1] = new NavItem(myRoot, labelList[_loc_1], menuList[_loc_1], _loc_1);
                btnsArray[_loc_1].addEventListener(MouseEvent.MOUSE_OVER, btnRollOver, false, 0, true);
                btnsArray[_loc_1].addEventListener(MouseEvent.MOUSE_OUT, btnRollOut, false, 0, true);
                btnsArray[_loc_1].addEventListener(MouseEvent.CLICK, btnClick, false, 0, true);
                addChild(btnsArray[_loc_1]);
                buildSubCol(_loc_1);
                _loc_1 = _loc_1 + 1;
                doResize();
            }
            labelList = null;
            menuList = null;
            return;
        }

        public function fadeInShellAuto(param1:int) : void
        {
            visible = true;
            TweenMax.to(this, fadeSpeed, {alpha:1, overwrite:true, delay:fadeSpeed});
            switchColsAuto(param1);
            return;
        }

        function buildNewsCol() : void
        {
            btnsArray = new Array();
            newsMenu = new NewsMenu(myRoot, fadeSpeed, myXMLList);
            btnsArray.push(newsMenu);
            addChild(newsMenu);
            return;
        }

        function onResizer(event:Event)
        {
            doResize();
            return;
        }

        function switchCols(param1:Number) : void
        {
            var _loc_2:int = 0;
            if (name != "newsMenu")
            {
                fadeBtns();
                trace("myTier " + myTier + ", btnsArray[currNavId].field.text " + btnsArray[currNavId].field.text);
                myRoot.updateMenuArray(myTier, btnsArray[currNavId].field.text);
                if (navMenuArray.length > 0)
                {
                    _loc_2 = 0;
                    while (_loc_2 < navMenuArray.length)
                    {
                        
                        if (_loc_2 == param1)
                        {
                            navMenuArray[_loc_2].visible = true;
                            navMenuArray[_loc_2].fadeInShell();
                        }
                        else if (navMenuArray[_loc_2] != undefined)
                        {
                            navMenuArray[_loc_2].fadeOutShell();
                        }
                        _loc_2++;
                    }
                }
            }
            else
            {
                myRoot.updateMenuArray("news", "news");
                newsMenu.fadeInShell();
            }
            return;
        }

        function toLowercase(param1:String) : String
        {
            var _loc_2:String = null;
            var _loc_3:* = undefined;
            _loc_2 = "";
            _loc_3 = 0;
            while (_loc_3 < param1.length)
            {
                
                if (param1.charAt(_loc_3) == " ")
                {
                    _loc_2 = _loc_2 + "-";
                }
                else
                {
                    _loc_2 = _loc_2 + param1.charAt(_loc_3).toLowerCase();
                }
                _loc_3 = _loc_3 + 1;
            }
            return _loc_2;
        }

        function fadeBtns() : void
        {
            var _loc_1:int = 0;
            _loc_1 = 0;
            while (_loc_1 < btnsArray.length)
            {
                
                if (_loc_1 != currNavId)
                {
                    TweenMax.to(btnsArray[_loc_1].field, fadeSpeed, {tint:myRoot.coldColor, overwrite:true});
                }
                else
                {
                    TweenMax.to(btnsArray[_loc_1].field, fadeSpeed, {tint:myRoot.hotColor, overwrite:true});
                }
                _loc_1++;
            }
            return;
        }

        function btnRollOut(event:MouseEvent) : void
        {
            if (event.currentTarget.idNum != currNavId)
            {
                TweenMax.to(event.currentTarget.field, fadeSpeed, {tint:myRoot.coldColor, overwrite:true});
            }
            return;
        }

        function switchColsAuto(param1:Number) : void
        {
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            if (name == "newsMenu")
            {
                newsMenu.fadeInShell();
            }
            else
            {
                _loc_2 = btnsArray.length;
                _loc_3 = 0;
                while (_loc_3 < _loc_2)
                {
                    
                    if (toLowercase(btnsArray[_loc_3].field.text) == myRoot.pathNames[param1])
                    {
                        currNavId = btnsArray[_loc_3].idNum;
                        TweenMax.to(btnsArray[_loc_3].field, fadeSpeed, {tint:myRoot.hotColor, overwrite:true});
                        navMenuArray[_loc_3].visible = true;
                        _loc_4 = param1 + 1;
                        navMenuArray[_loc_3].fadeInShellAuto((param1 + 1));
                    }
                    else
                    {
                        TweenMax.to(btnsArray[_loc_3].field, fadeSpeed, {tint:myRoot.coldColor, overwrite:true});
                        navMenuArray[_loc_3].fadeOutShell();
                    }
                    _loc_3++;
                }
            }
            return;
        }

        function btnRollOver(event:MouseEvent) : void
        {
            if (event.currentTarget.idNum != currNavId)
            {
                TweenMax.to(event.currentTarget.field, fadeSpeed, {tint:myRoot.overColor, overwrite:true});
            }
            else
            {
                TweenMax.to(event.currentTarget.field, fadeSpeed, {tint:myRoot.hotColor, overwrite:true});
            }
            return;
        }

        function btnClick(event:MouseEvent) : void
        {
            currNavId = event.currentTarget.idNum;
            if (navMenuArray[currNavId] == undefined)
            {
                doResize();
                switchCols(event.currentTarget.idNum);
            }
            else
            {
                switchCols(event.currentTarget.idNum);
            }
            return;
        }

        function buildBookmarksCol() : void
        {
            btnsArray = new Array();
            newsMenu = new NewsMenu(myRoot, fadeSpeed, myXMLList);
            btnsArray.push(newsMenu);
            addChild(newsMenu);
            return;
        }

        function identifyCol() : void
        {
            switch(myTier)
            {
                case "tier0":
                {
                    switch(name)
                    {
                        case "projectsMenu":
                        {
                            buildCol();
                            break;
                        }
                        case "practiceMenu":
                        {
                            buildCol();
                            break;
                        }
                        case "contactMenu":
                        {
                            buildCol();
                            break;
                        }
                        case "newsMenu":
                        {
                            buildNewsCol();
                            break;
                        }
                        case "bookmarksMenu":
                        {
                            buildCol();
                            break;
                        }
                        default:
                        {
                            break;
                        }
                    }
                    break;
                }
                case "tier1":
                {
                    switch(name)
                    {
                        case "architectureMenu":
                        {
                            buildCol();
                            break;
                        }
                        case "productsMenu":
                        {
                            buildCol();
                            break;
                        }
                        case "exhibitionsMenu":
                        {
                            buildCol();
                            break;
                        }
                        case "pressMenu":
                        {
                            buildCol();
                            break;
                        }
                        case "employMenu":
                        {
                            buildCol();
                            break;
                        }
                        case "staffMenu":
                        {
                            buildCol();
                            break;
                        }
                        case "publicationsMenu":
                        {
                            buildCol();
                            break;
                        }
                        default:
                        {
                            break;
                        }
                    }
                    break;
                }
                case "tier2":
                {
                    switch(name)
                    {
                        case "typeMenu":
                        {
                            buildCol();
                            break;
                        }
                        case "dateMenu":
                        {
                            buildCol();
                            break;
                        }
                        case "continentMenu":
                        {
                            buildCol();
                            break;
                        }
                        case "publicationsMenu":
                        {
                            buildCol();
                            break;
                        }
                        case "staffSubMenu":
                        {
                            buildCol();
                            break;
                        }
                        default:
                        {
                            break;
                        }
                    }
                    break;
                }
                case "tier3":
                {
                    switch(name)
                    {
                        case "countryMenu":
                        {
                            buildCol();
                            break;
                        }
                        default:
                        {
                            break;
                        }
                    }
                    break;
                }
                case "tier4":
                {
                    switch(name)
                    {
                        case "provinceMenu":
                        {
                            buildCol();
                            break;
                        }
                        case "paginatedMenu":
                        {
                            buildCol();
                            break;
                        }
                        default:
                        {
                            break;
                        }
                    }
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }

        public function fadeInShell() : void
        {
            if (name == "newsMenu")
            {
            }
            visible = true;
            TweenMax.to(this, fadeSpeed, {alpha:1, overwrite:true, delay:fadeSpeed});
            autoClick();
            return;
        }

    }
}
